const express = require("express");
const bodyParser = require("body-parser")
const cookieParser = require("cookie-parser")
const cookieSession = require("cookie-session")
const multer = require("multer");

//1.创建express的实例对象
const app = express();

//2.配置中间
app.use(bodyParser.urlencoded({
    extended: false,
    limit: "500mb"
}))
app.use(bodyParser.json());
app.use(cookieParser("useker.cn"));
app.use(cookieSession({
    name: "useker.cn",
    keys: ["aaa", "bbb"],
    maxAge: 1000 * 60 * 20
}))

const upload = multer({
    dest: "./../src/upload/"
});
app.use(upload.any());

app.use("*", (request, response, next) => {
    request.header("Content-Type", "application/json;charset=utf-8");
    // 判断是否路径在于白名单中 , 拦截器
    if (request.originalUrl.indexOf("/shopping") == -1) {
        next();
    } else {
        // console.log(request.session["users"]);
        if (!request.session["users"]) {
            response.json({
                msg: "请先登录",
                status: -1,
            })
            //服务器重定向 response.redirect("./login.html");
        } else {
            next();
        }
    }

})
//静态资源服务器
app.use(express.static("./../src"));


//挂载api路由对象 (保安大爷)

app.use("/api", require("./routes/ApiRoute").apiRouter)


app.listen(8081, () => {
    console.log("服务启动完毕,http://127.0.0.1:8081");
})